package GerenciadorConexao;

import Erro.ConexaoException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

/**
 *
 * @author Tiago
 */
public class GerenciadorConexaoMYsql implements GerenciadorConexao{

   private static GerenciadorConexao instancia;
    private String driver;
    private String url;
    private String usu;
    private String sen;
        
    /**
     * Previne a geração de novas instâncias
     */
    private GerenciadorConexaoMYsql(){
        ResourceBundle rb = ResourceBundle.getBundle("Help_Disk.banco.banco");
        driver = rb.getString("driver");
        url = rb.getString("url");
        usu = rb.getString("usu");
        sen = rb.getString("sen");
    }
    
    /**
     * Implementa o padrão Singleton
     * @return 
     */
    public static GerenciadorConexao getInstancia(){
        if(instancia==null) {
            instancia=new GerenciadorConexaoMYsql();
        }
        return instancia;
    }
    
    @Override
    public Connection conectar() throws ConexaoException{
        Connection c;
        try {
            Class.forName(driver);
            c = DriverManager.getConnection(url, usu, sen);
        } catch (SQLException | ClassNotFoundException e) {
            throw new ConexaoException(e);
        }
        return c;
    }

    @Override
    public void desconectar(Connection c) throws ConexaoException{
        try {
            c.close();
        } catch (SQLException e) {
            throw new ConexaoException(e);
        }
    }
    
}
